# 2352. 相等行列对
# 中等
# 相关标签
# 相关企业
# 提示
# 给你一个下标从 0 开始、大小为 n x n 的整数矩阵 grid ，返回满足 Ri 行和 Cj 列相等的行列对 (Ri, Cj) 的数目。
#
# 如果行和列以相同的顺序包含相同的元素（即相等的数组），则认为二者是相等的。


class Solution(object):
    def equalPairs(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        if grid is None or len(grid) == 0:
            return 0

        grid_length = len(grid)
        x_str, y_str = [""] * grid_length, [""] * grid_length
        for i in range(0, grid_length):
            if grid[i] is None or len(grid[i]) != grid_length:
                return 0
            for j in range(0, grid_length):
                x_str[i] = x_str[i] + "_" + str(grid[i][j])
                y_str[j] = y_str[j] + "_" + str(grid[i][j])

        for i in range(0, grid_length):
            x_str[i] = str(hash(x_str[i]))
            y_str[i] = str(hash(y_str[i]))

        same_count = 0
        for i in range(0, grid_length):
            for j in range(0, grid_length):
                if x_str[i] == y_str[j]:
                    same_count = same_count + 1

        return same_count
